package com.outsourcing.bottle.widget;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
/**
 * 自定义的ListView
 * @author 06peng
 *
 */
public class CustomScrollListView extends ListView {
	
	private List<Object> mListData;
	private List<View> mAllListView;
	private InitScrollListViewListener listener;
	
	public CustomScrollListView(Context context) {
		super(context);
	}
	
	public CustomScrollListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public CustomScrollListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	
	public interface InitScrollListViewListener {
		public void initListView(List<View> mAllListView, List<Object> mListData);
	}
	
	public void setInitScrollListViewListener(InitScrollListViewListener listener) {
		this.listener = listener;
	}
	
	private void initListView(List<View> mAllListView, List<Object> mListData) {
		if (listener != null) {
			listener.initListView(mAllListView, mListData);
		}
	}
	
	public void setAdapter() {
		if (mAllListView == null) {
			mAllListView = new ArrayList<View>();
		}
		if (mListData == null) {
			mListData = new ArrayList<Object>();
		}
		initListView(mAllListView, mListData);
		if (!mAllListView.isEmpty()) {
			ScrollListAdapter apdater = new ScrollListAdapter();
			setAdapter(apdater);
		}
	}
	
	public class ScrollListAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return mListData == null ? 0 : mListData.size();
		}

		@Override
		public Object getItem(int position) {
			return mListData == null ? null : mListData.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			return mAllListView == null ? null : mAllListView.get(position);
		}
		
	}

}
